New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tonal-note

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tonal-note

Parse and manipulate music notes in scientific notation

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
913
decreased by-24.36%
Maintainers
1
Weekly downloads
 
Created
Source

Note

npm version tonal

tonal-note is a collection of functions to manipulate musical notes in scientific notation

This is part of tonal music theory library.

Usage

import * as Note from "tonal-note"
// or const Note = require("tonal-note")
Note.name("bb2") // => "Bb2"
Note.chroma("bb2") // => 10
Note.midi("a4") // => 69
Note.freq("a4") // => 440
Note.oct("G3") // => 3

// part of tonal
const Tonal = require("tonal")
// or import Note from "tonal"
Tonal.Note.midi("d4") // => 62

Install

npm install tonal-note

API Documentation

Note.namesArray

Get a list of note names (pitch classes) within a octave

Kind: static constant of Note

ParamTypeDescription
accTypesstring(Optional, by default " b#"). A string with the accidentals types: " " means no accidental, "#" means sharps, "b" mean flats, can be conbined (see examples)

Example

Note.names(" b") // => [ "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" ]
Note.names(" #") // => [ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" ]

Note.propsObject

Get note properties. It returns an object with the following information:

  • name {String}: the note name. The letter is always in uppercase
  • letter {String}: the note letter, always in uppercase
  • acc {String}: the note accidentals
  • octave {Number}: the octave or null if not present
  • pc {String}: the pitch class (letter + accidentals)
  • step {Number}: number equivalent of the note letter. 0 means C ... 6 means B.
  • alt {Number}: number equivalent of accidentals (negative are flats, positive sharps)
  • chroma {Number}: number equivalent of the pitch class, where 0 is C, 1 is C# or Db, 2 is D...
  • midi {Number}: the note midi number
  • freq {Number}: the frequency using an equal temperament at 440Hz

This function always returns an object with all this properties, but if it"s not a valid note all properties will be null.

The returned object can"t be mutated.

Kind: static constant of Note
Returns: Object - an object with the properties (or an object will all properties set to null if not valid note)

ParamTypeDescription
noteStringthe note name in scientific notation

Example

Note.props("fx-3").name // => "F##-3"
Note.props("invalid").name // => null
Note.props("C#3").oct // => 3
Note.props().oct // => null

Note.midiToFreqNumber

Get the frequency from midi number

Kind: static constant of Note
Returns: Number - the frequency or null if not valid note midi

ParamTypeDescription
midiNumberthe note midi number
tuningNumber(Optional) 440 by default

Note.freqToMidiNumber

Get the midi number from a frequency in hertz. The midi number can contain decimals (with two digits precission)

Kind: static constant of Note

ParamType
frequencyNumber

Example

Note.freqToMidi(220)); //=> 57;
Note.freqToMidi(261.62)); //=> 60;
Note.freqToMidi(261)); //=> 59.96;

Note.chromaInteger

Return the chroma of a note. The chroma is the numeric equivalent to the pitch class, where 0 is C, 1 is C# or Db, 2 is D... 11 is B

Kind: static constant of Note
Returns: Integer - the chroma number

ParamTypeDescription
notestringthe note name

Example

Note.chroma("Cb") // => 11
["C", "D", "E", "F"].map(Note.chroma) // => [0, 2, 4, 5]

Note.stepToLetterstring

Given a step number return it"s letter (0 = C, 1 = D, 2 = E)

Kind: static constant of Note
Returns: string - the letter

ParamType
stepnumber

Example

Note.stepToLetter(3) // => "F"

Note.altToAccString

Given an alteration number, return the accidentals

Kind: static constant of Note

ParamType
altNumber

Example

Note.altToAcc(-3) // => "bbb"

Note.build

Deprecated. This is kept for backwards compatibility only. Use Note.from instead

Kind: static constant of Note

Note.simplifyString

Simplify the note: find an enhramonic note with less accidentals.

Kind: static constant of Note
Returns: String - the simplfiied note or null if not valid note

ParamTypeDescription
noteStringthe note to be simplified
useSameAccTypeboolean(optional, true by default) set to true to ensure the returned note has the same accidental types that the given note

Example

Note.simplify("C##") // => "D"
Note.simplify("C###") // => "D#"
Note.simplify("C###", false) // => "Eb"
Note.simplify("B#4") // => "C5"

Note.enharmonicString

Get the simplified and enhramonic note of the given one.

Kind: static constant of Note
Returns: String - the enhramonic note

ParamType
noteString

Example

Note.enharmonic("Db") // => "C#"
Note.enhramonic("C") // => "C"

Note.tokenize(str)Array

Split a string into tokens related to note parts. It returns an array of strings [letter, accidental, octave, modifier]

It always returns an array

Kind: static method of Note
Returns: Array - an array of note tokens

ParamType
strString

Example

Note.tokenize("C#2") // => ["C", "#", "2", ""]
Note.tokenize("Db3 major") // => ["D", "b", "3", "major"]
Note.tokenize("major") // => ["", "", "", "major"]
Note.tokenize("##") // => ["", "##", "", ""]
Note.tokenize() // => ["", "", "", ""]

Note.name()string

Given a note name, return the note name or null if not valid note. The note name will ALWAYS have the letter in upercase and accidentals using # or b

Can be used to test if a string is a valid note name.

Kind: static method of Note

Type
Pitch | string

Example

Note.name("cb2") // => "Cb2"
["c", "db3", "2", "g+", "gx4"].map(Note.name) // => ["C", "Db3", null, null, "G##4"]

Note.pc()string

Get pitch class of a note. The note can be a string or a pitch array.

Kind: static method of Note
Returns: string - the pitch class

Type
string | Pitch

Example

Note.pc("Db3") // => "Db"
["db3", "bb6", "fx2"].map(Note.pc) // => [ "Db", "Bb", "F##"]

Note.midi(note)Integer

Get the note midi number (an alias of tonal-midi toMidi function)

Kind: static method of Note
Returns: Integer - the midi number or null if not valid pitch
See: midi.toMidi

ParamTypeDescription
notestring | Numberthe note to get the midi number from

Example

Note.midi("C4") // => 60
Note.midi(60) // => 60

Note.freq(note)Number

Get the frequency of a note

Kind: static method of Note
Returns: Number - the frequency

ParamTypeDescription
notestring | Numberthe note name or midi note number

Example

Note.freq("A4") // => 440
Note.freq(69) // => 440

Note.oct(note)Integer

Get the octave of the given pitch

Kind: static method of Note
Returns: Integer - the octave or null if doesn"t have an octave or not a valid note

ParamTypeDescription
notestringthe note

Example

Note.oct("C#4") // => 4
Note.oct("C") // => null
Note.oct("blah") // => undefined

Note.from(props, [baseNote])String

Creates a note name in scientific notation from note properties, and optionally another note name. It receives an object with:

  • step: the note step (0 = C, 1 = D, ... 6 = B)
  • alt: (optional) the alteration. Negative numbers are flats, positive sharps
  • oct: (optional) the octave

Optionally it receives another note as a "base", meaning that any prop not explicitly received on the first parameter will be taken from that base note. That way it can be used as an immutable "set" operator for a that base note

Kind: static method of Note
Returns: String - the note name in scientific notation or null if not valid properties

ParamTypeDescription
propsObjectthe note properties
[baseNote]Stringnote to build the result from. If given, it returns the result of applying the given props to this note.

Example

Note.from({ step: 5 }) // => "A"
Note.from({ step: 1, acc: -1 }) // => "Db"
Note.from({ step: 2, acc: 2, oct: 2 }) // => "E##2"
Note.from({ step: 7 }) // => null
Note.from({alt: 1, oct: 3}, "C4") // => "C#3"

Note.fromMidi(midi, useSharps)string

Given a midi number, returns a note name. The altered notes will have flats unless explicitly set with the optional useSharps parameter.

Kind: static method of Note
Returns: string - the note name

ParamTypeDescription
midinumberthe midi note number
useSharpsboolean(Optional) set to true to use sharps instead of flats

Example

Note.fromMidi(61) // => "Db4"
Note.fromMidi(61, true) // => "C#4"
// it rounds to nearest note
Note.fromMidi(61.7) // => "D4"

Keywords

FAQs

Package last updated on 04 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc